package gui;

import java.util.ArrayList;
import java.util.Collections;

import org.Consts;
import org.TempVariables;
import org.Utility;
import org.json.JSONException;
import org.json.JSONObject;

import com.ydev.graphics.animations.R;

import CustomUI.AddPartsDialog;
import CustomUI.AssembleView;
import CustomUI.ydButton;
import CustomUI.ydImageCell;
import DTO.ObjectDTO;
import DTO.PartDTO;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class Assemble extends Activity {
	
	ydButton btnPlus;
	ydButton btnUp;
	ydButton btnDown;
	ydButton btnRight;
	AssembleView av;
	
	PartDTO selectedPart;
	ydImageCell selectedCell;
	
	LinearLayout llAssembleParts;
	
	public AddPartsDialog apd;

	  @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.assemble);
	        
	        init();
	  }
	  
	  public void init(){
	
	  btnPlus=(ydButton)findViewById(R.id.ydButtonPlus);
	  btnDown=(ydButton)findViewById(R.id.ydButtonDown);
	  btnUp=(ydButton)findViewById(R.id.ydButtonUp);
	  btnRight=(ydButton)findViewById(R.id.ydButtonRight);
	  av=(AssembleView)findViewById(R.id.assembleView1);
	  apd=new AddPartsDialog(this,Assemble.this);
	  
	  llAssembleParts=(LinearLayout)findViewById(R.id.llAssembleParts);
	  
	  btnPlus.addOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			apd.show();
			
		}
	});
	  
	  btnDown.addOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				PartDTO parent =selectedPart.Parent;
				if(parent!=null){
					int i=0;
					Boolean isDone=false;
					for (PartDTO p:parent.ChildernTop){
						if(p==selectedPart){
							isDone=true;
							if(i==0){
								parent.ChildernBottom.add(p);
								parent.ChildernTop.remove(p);
								break;
							}
							else{
								Collections.swap(parent.ChildernTop, i, i-1);
								break;
							}
						}
						i++;
					}
					i=0;
					if(!isDone){
						for (PartDTO p:parent.ChildernBottom){
							if(p==selectedPart){
									if(parent.ChildernBottom.size()-1>=i+1)
									Collections.swap(parent.ChildernBottom, i, i+1);
							}
							i++;
						}
					}
					reLoadSide();
				}
				
			}
		});
	  
	  btnUp.addOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				PartDTO parent =selectedPart.Parent;
				if(parent!=null){
					int i=0;
					Boolean isDone=false;
					for (PartDTO p:parent.ChildernBottom){
						if(p==selectedPart){
							isDone=true;
							if(i==0){
								parent.ChildernTop.add(p);
								parent.ChildernBottom.remove(p);
								break;
							}
							else{
								Collections.swap(parent.ChildernBottom, i, i-1);
								break;
							}
						}
						i++;
					}
					i=0;
					if(!isDone){
						for (PartDTO p:parent.ChildernTop){
							if(p==selectedPart){
									if(parent.ChildernTop.size()-1>=i+1)
									Collections.swap(parent.ChildernTop, i, i+1);
							}
							i++;
						}
					}
					reLoadSide();
				}
				
			}
		});
	  
	  av.addOnTouchListeners(new OnTouchListener() {
		
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			float x = event.getX();
			float y = event.getY();
			if(selectedPart!=null){
				selectedPart.MoveTo(x, y);
				selectedPart.recMoveTo(selectedPart.delta.x, selectedPart.delta.y);
			}
			//av.update();
			av.invalidate();
			return true;
		}
	});
		  
	  }

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode==RESULT_OK){
			av.invalidate();
			if(requestCode==3){
				Boolean isChanged=data.getBooleanExtra("isChanged", false);
				if(isChanged){
					if(selectedPart==null){
						av.addPart(TempVariables.partDTO);
					}
					else{
						TempVariables.partDTO.Parent=selectedPart;
						selectedPart.ChildernTop.add(TempVariables.partDTO);
						
					}
				}
			}
		}
		return;
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		reLoadSide();
		
	}
	
	public void reLoadSide(){
		llAssembleParts.removeAllViews();
		ArrayList<PartDTO> parts=av.getParts();
		setSideBarTree(parts, 0);
		av.invalidate();
	}
	
	private void setSideBarTree(ArrayList<PartDTO> parts, int layer) {
		ydImageCell cell;
		int i=0;
		//for(int a=parts.size()-1; a>=0; a--){
		for(final PartDTO p:parts){
			setSideBarTree(getReversed(p.ChildernTop), layer+1);
			cell  = new ydImageCell(getApplicationContext());
			cell.setImage(p.image);
			cell.setSize(50,50);
			if(layer>0){
				cell.addToLeftMargin(layer*10);
				cell.setBarColor(layer);
			}
			if(selectedPart!=null){
				if(selectedPart==p)
					cell.setCellSelected(true);
			}
			else if(i==0){
				cell.setCellSelected(true);
				selectedPart = p;
				selectedCell=cell;
			}
			i++;
			final ydImageCell c=cell;
			cell.addOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					
					selectedPart = p;
					selectedCell.setCellSelected(false);
					selectedCell=c;
					selectedCell.setCellSelected(true);
				}
			});
			llAssembleParts.addView(cell);
			setSideBarTree(p.ChildernBottom, layer+1);
		}
		
	}
	
	public ArrayList<PartDTO> getReversed(ArrayList<PartDTO> original)
	{
	ArrayList<PartDTO> copy = new ArrayList<PartDTO>(original);
	Collections.reverse(copy);
	return copy;
	}

	private void setSideBar(ArrayList<PartDTO> parts, int layer) {
		ydImageCell cell;
		int i=0;
		for(final PartDTO p:parts){
			
			cell  = new ydImageCell(getApplicationContext());
			cell.setImage(p.image);
			cell.setSize(50,50);
			if(layer>0){
				cell.addToLeftMargin(layer*50);
				cell.setBarColor(layer);
			}
			if(selectedPart!=null){
				if(selectedPart==p)
					cell.setCellSelected(true);
			}
			else if(i==0){
				cell.setCellSelected(true);
				selectedPart = p;
				selectedCell=cell;
			}
			i++;
			final ydImageCell c=cell;
			cell.addOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					
					selectedPart = p;
					selectedCell.setCellSelected(false);
					selectedCell=c;
					selectedCell.setCellSelected(true);
				}
			});
			llAssembleParts.addView(cell);
			setSideBar(p.Childern, layer+1);
		}
		
	}

}
